<?php
declare (strict_types=1);

namespace app\common\tool\Swoole\Client;

use Swoole\Client;

class TCP
{
    public $type = SWOOLE_SOCK_TCP;

    public $config;

    public function __construct()
    {
        $this->config = config('server');
    }

    /**
     * @param $class - 类
     * @param $action - 方法名
     * @param $arguments - 参数
     * @return array
     * 原点 2023年2月14日10:00:03 创建客户端投递Task任务
     */
    public function task($class, $action, $arguments)
    {
        $client = new Client($this->type);
        if (!$client->connect($this->config['host'], $this->config['port'], -1)) {
            return ['code' => -1, 'msg' => '服务器连接失败，无法创建推送任务:' . $client->errCode];
        }
        $client->send(json_encode(['class' => $class, 'action' => $action, 'arguments' => $arguments]));
        $client->close();
        return ['code' => 1, 'msg' => '已投递值Task任务队列'];
    }


}
